import { _decorator, Component, instantiate, Node, Prefab, Vec2, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('CityButton')
export class CityButton extends Component {
    // 添加
    @property(Prefab)
    AddPrefab: Prefab = null;
    // 详情
    @property(Prefab)
    DetailPrefab: Prefab = null;
    // 升级
    @property(Prefab)
    UpgradePrefab: Prefab = null;
    // 训练
    @property(Prefab)
    TrainPrefab: Prefab = null;
    // 征税
    @property(Prefab)
    MoneyPrefab: Prefab = null;
    // 研究
    @property(Prefab)
    ReadPrefab: Prefab = null;
    // 显示层为了显示预制体的时候不被其他节点遮挡
    @property(Node)
    ShowNode: Node = null;

    // 存放预制体数组
    prefabArr: Array<Prefab> = [];

    ButtonClick(event: any) {
        const target = event.target; // 获取点击的节点
        const buttonName = target.name; // 获取按钮名称
        const touchPos = event.target.worldPosition; // 获取点击位置
        // 根据按钮名称，将对应的预制体存入数组
        switch (buttonName) {
            case 'bighouse':
                this.prefabArr = [this.AddPrefab, this.DetailPrefab, this.UpgradePrefab]; // 将预制体存入数组
                break;
            case 'smallhouse':
                this.prefabArr = [this.AddPrefab, this.ReadPrefab, this.UpgradePrefab]; // 将预制体存入数组
                break;
            case 'coast':
                this.prefabArr = [this.AddPrefab, this.TrainPrefab, this.UpgradePrefab]; // 将预制体存入数组
                break;
            case 'beautyhouse':
                this.prefabArr = [this.AddPrefab, this.MoneyPrefab, this.UpgradePrefab]; // 将预制体存入数组
                break;
            case 'ground':
                this.prefabArr = [this.AddPrefab, this.UpgradePrefab]; // 将预制体存入数组
                break;
            case 'smalground':
                this.prefabArr = [this.AddPrefab, this.UpgradePrefab]; // 将预制体存入数组
                break;
            case 'treehouse':
                this.prefabArr = [this.AddPrefab, this.UpgradePrefab]; // 将预制体存入数组
                break;
            case 'snowhouse':
                this.prefabArr = [this.AddPrefab, this.UpgradePrefab]; // 将预制体存入数组
                break;
            default:
                break;
        }

        this.ShowNode.removeAllChildren(); // 清空节点下的所有子节点

        for (let i = 0; i < this.prefabArr.length; i++) {
            const node = instantiate(this.prefabArr[i]); // 实例化预制体
            node.setScale(0.3, 0.3); // 设置添加的节点的大小
            this.ShowNode.addChild(node); // 将添加的节点添加到显示层
            const woldPos = new Vec3(touchPos.x - 80 + i * 80, touchPos.y + (i == 1 ? -50 : 0), 0);
            node.setWorldPosition(woldPos); // 设置添加的节点的位置
        }
    }
}


